<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单ID" prop="orderId">
        <el-input
            v-model="queryParams.orderId"
            placeholder="请输入订单ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item  label="评分" prop="rating">
        <el-input
            v-model="queryParams.rating"
            placeholder="请输入评分（一星输入1）"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['order:evaluation:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>


    <el-table v-loading="loading" :data="evaluationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="评价ID" align="center" prop="evaluationId" />
      <!--      <el-table-column label="订单ID" align="center" prop="oId" />-->
      <!--      <el-table-column label="用户ID" align="center" prop="uId" />-->
      <!--      <el-table-column label="商家ID" align="center" prop="mId" />-->
      <!--      <el-table-column label="服务ID" align="center" prop="smId" />-->
      <el-table-column label="服务名称" align="center" prop="name" />
      <el-table-column label="评价时间" align="center" prop="evalTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.evalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="评价内容" align="center" prop="content" />
      <el-table-column label="评分" align="center" prop="rating">
        <template #default="scope">
          <dict-tag :options="rating" :value="scope.row.rating"/>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="images" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.images" :width="50" :height="50"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="状态：1-显示，0-隐藏" align="center" prop="status">-->
<!--        <template #default="scope">-->
<!--          <dict-tag :options="status" :value="scope.row.status"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="逻辑删除：1-已删除，0-未删除" align="center" prop="isDeleted">-->
<!--        <template #default="scope">-->
<!--          <dict-tag :options="is_deleted" :value="scope.row.isDeleted"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['order:evaluation:edit']">修改</el-button>
<!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['order:evaluation:remove']">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改评价对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="evaluationRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="评价时间" prop="evalTime">
          <el-date-picker
              clearable
              v-model="form.evalTime"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择评价时间"
              :disabled="true"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="评价内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" :disabled="true" />
        </el-form-item>


        <el-form-item label="图片" prop="images">
          <image-upload v-model="form.images" :disabled="true"/>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-select
              v-model="form.status"
              placeholder="请选择状态"
              :disabled="isSubmitting"
          >
          <el-option
              v-for="dict in statusOptions"
          :key="dict.value"
          :label="dict.label"
          :value="dict.value"
          ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="逻辑删除" prop="isDeleted">
          <el-select
              v-model="form.isDeleted"
              placeholder="请选择逻辑删除"
              :disabled="isSubmitting"
          >
            <el-option
                v-for="dict in isDeletedOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm" :loading="isSubmitting">
            确 定
          </el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Evaluation">
import { listEvaluation, getEvaluation, delEvaluation, addEvaluation, updateEvaluation } from "@/api/order/evaluation"
import {parseTime} from "../../../utils/ruoyi.js";

const { proxy } = getCurrentInstance()
const { is_deleted, status, rating } = proxy.useDict('is_deleted', 'status', 'rating')

const evaluationList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    evaluationId:null,
    pageNum: 1,
    pageSize: 10,
    orderId: null,
    userId: null,
    merchantId: null,
    smId: null,
    evalTime: null,
    content: null,
    rating: null,
    images: null,
    status: null,
    isDeleted: null,
    name: null,
    userName:null
  },
  rules: {
  }
})


const statusOptions = [
  { value: 1, label: '显示' },
  { value: 0, label: '隐藏' }
];

const isDeletedOptions = [
  { value: 1, label: '已删除' },
  { value: 0, label: '未删除' }
];

const { queryParams, form, rules } = toRefs(data)

/** 查询评价列表 */
function getList() {
  loading.value = true
  listEvaluation(queryParams.value).then(response => {
    evaluationList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    evaluationId: null,
    orderId: null,
    userId: null,
    merchantId: null,
    smId: null,
    evalTime: null,
    content: null,
    rating: null,
    images: null,
    status: null,
    isDeleted: null
  }
  proxy.resetForm("evaluationRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.evaluationId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加评价"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _eId = row.evaluationId || ids.value
  getEvaluation(_eId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改评价"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["evaluationRef"].validate(valid => {
    if (valid) {
      if (form.value.evaluationId != null) {
        updateEvaluation(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addEvaluation(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _eIds = row.evaluationId || ids.value
  proxy.$modal.confirm('是否确认删除评价编号为"' + _eIds + '"的数据项？').then(function() {
    return delEvaluation(_eIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('order/evaluation/export', {
    ...queryParams.value
  }, `evaluation_${new Date().getTime()}.xlsx`)
}

getList()



</script>


